package com.qf.business.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.goods.mapper.GoodsInfoMapper;
import com.qf.business.goods.service.*;
import com.qf.commons.event.apply.producer.EventUtils;
import com.qf.commons.redis.cache.annotation.CacheGet;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.goods.dto.GoodsGuigeDto;
import com.qf.data.goods.dto.GoodsInfoDto;
import com.qf.data.goods.entity.GoodsGuigeMiddle;
import com.qf.data.goods.entity.GoodsImage;
import com.qf.data.goods.entity.GoodsInfo;
import com.qf.data.goods.vo.input.GoodsInput;
import com.qf.data.goods.vo.output.GoodsFenmianOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * (GoodsInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-07-06 14:13:37
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsImageService goodsImageService;

    @Autowired
    private GoodsGuigeMiddleService goodsGuigeMiddleService;

    @Autowired
    private GoodsGuigeService goodsGuigeService;

    @Autowired
    private GoodsGuigeValService guigeValService;

    /**
     * 后端系统 - 查询商品列表
     * @return
     */
    @Override
    public List<GoodsFenmianOutput> querySystemList() {
        return goodsInfoMapper.querySystemList();
    }

    /**
     * 保存商品信息
     * @param input
     * @return
     */
    @Override
    @Transactional
    public int insert(GoodsInput input) {
        //保存商品
        GoodsInfo goodsInfo = QfBeanUtils.beanCopy(input, GoodsInfo.class);
        super.save(goodsInfo);

        //生成DTO对象
        GoodsInfoDto goodsInfoDto = QfBeanUtils.beanCopy(goodsInfo, GoodsInfoDto.class);

        //保存商品的图片信息
        String imagesJson = input.getImages();// [{image:xxxx,info:xxxx,type:0},{}]
        List<GoodsImage> images = JSON.parseArray(imagesJson, GoodsImage.class);
        
        //循环保存图片信息
        for (GoodsImage image : images) {
            image.setGid(goodsInfo.getId());
            //保存image对象
            goodsImageService.save(image);

            //将封面信息 设置到DTO对象中
            if (image.getType() == 1) {
                goodsInfoDto.setImage(image.getImage());
            }
        }

        //保存商品的规格信息
        String guiges = input.getGuiges();
        List<GoodsGuigeMiddle> goodsGuigeMiddles = JSON.parseArray(guiges, GoodsGuigeMiddle.class);

        //规格的DTO集合
        List<GoodsGuigeDto> guigeDtos = new ArrayList<>();
        goodsInfoDto.setGuiges(guigeDtos);

        //设置商品id
        for (GoodsGuigeMiddle goodsGuigeMiddle : goodsGuigeMiddles) {
            goodsGuigeMiddle.setGoodsId(goodsInfo.getId());
            goodsGuigeMiddleService.save(goodsGuigeMiddle);

            //创建规格的DTO
            GoodsGuigeDto goodsGuigeDto = new GoodsGuigeDto();
            goodsGuigeDto.setGuigeName(goodsGuigeService.getById(goodsGuigeMiddle.getGuigeId()).getGuigeName());
            goodsGuigeDto.setGuigeVal(guigeValService.getById(goodsGuigeMiddle.getValId()).getGuigeVal());
            guigeDtos.add(goodsGuigeDto);
        }

        //通过事件总线 将新增商品的信息 发布出去 ES
        EventUtils.send("goods_insert", goodsInfoDto);
        return 1;
    }

    @CacheGet("'cache_goodsitem_' + #gid")
    @Override
    public GoodsInfo queryItemByGid(Long gid) {
        GoodsInfo goodsInfo = super.getById(gid);
        //根据商品id查询图片信息
        List<GoodsImage> goodsImages = goodsImageService.query()
                .eq("gid", gid)
                .orderByDesc("type")
                .list();
        goodsInfo.setGoodsImages(goodsImages);
        return goodsInfo;
    }
}


